package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.sql.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import BizLogic.BookingManager;

import model.BookingDTO;

import dao.DAOFactory;
import dao.FacilityDAO;
import dao.FacilityTypeDAO;
import dao.PriorityDAO;

/**
 * Servlet implementation class ManageBookingServlet
 */
@WebServlet("/ManageBookingServlet")
public class ManageBookingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ManageBookingServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request,response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response)throws ServletException,IOException {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date dt1=null;
		java.util.Date dt2=null;
		String result=null;
		try {
			dt1=sdf.parse(request.getParameter("datepicks"));
			System.out.println(dt1);
			dt2=sdf.parse(request.getParameter("datepick"));
			//sdf.applyPattern("YYYY-MM-DD");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpSession session=request.getSession();

		Date FromDate=Date.valueOf(sdf1.format(dt1));
		Date ToDate=Date.valueOf(sdf1.format(dt2));
		BookingDTO book=new BookingDTO();
		
		if(request.getParameter("priorities")!= " " ||request.getParameter("reason")!=" "  || ToDate !=null || FromDate!=null){
			if(FromDate.compareTo(ToDate)<=0)
			{
		book.setPriorityID(Integer.parseInt( request.getParameter("priorities")));
		book.setFacilityName(request.getParameter("facilityName"));
		book.setReason(request.getParameter("reason"));
		String staffID=String.valueOf(session.getAttribute("StaffID"));
		book.setStaffID(staffID);
		Date dt=new Date(new java.util.Date().getTime());
		book.setBookingDate(dt);
		book.setStatus("Pending");
		book.setToDate(ToDate);
		book.setFromDate(FromDate);
		BookingManager manager=new BookingManager();
		 result=manager.bookFacility(book,String.valueOf(session.getAttribute("StaffID")));
		}
			else
			{
				result="Dates must be in chronological order";
//				RequestDispatcher rd=request.getRequestDispatcher("LoadBookingServlet");
//				request.setAttribute("result", result);
//				rd.forward(request, response);
			}
		}
			
			else{
				result="Necessary Fields are left blank!!";
//				RequestDispatcher rd=request.getRequestDispatcher("LoadBookingServlet");
//				request.setAttribute("result", result);
//				rd.forward(request, response);
			}
		if(result.equalsIgnoreCase("Booking Successful"))
		{
			PrintWriter out=response.getWriter();
			out.println("<script>alert('Booking Successful')</script>");
			RequestDispatcher rd=request.getRequestDispatcher("ViewBookingServlet");
			rd.forward(request, response);
		}
		else
		{
			System.out.println(result);
			RequestDispatcher rd=request.getRequestDispatcher("LoadBookingServlet");
			request.setAttribute("result", result);
			rd.forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request,response);
	}

}
